<?php

namespace MoChat\Salesman\Action;

use Hyperf\DbConnection\Db;
use MoChat\Framework\Action\AbstractAction;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Common\Middleware\DashboardAuthMiddleware;
use MoChat\Framework\Request\ValidateSceneTrait;
use MoChat\Framework\Exception\CommonException;
use MoChat\Framework\Constants\ErrorCode;

/**
 * 开发客户任务数
 * @Controller
 */
class TaskClient extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class),
     * })
     * @RequestMapping(path="/dashboard/salesman/client", methods="GET")
     */
    public function handle()
    {
        $user = user();
        // 判断用户绑定企业信息
        if (!isset($user['corpIds']) || count($user['corpIds']) !== 1) {
            throw new CommonException(ErrorCode::INVALID_PARAMS, '未选择登录企业，不可操作');
        }
        $data = $this->request->all();
        $this->validated($data);
        $startTime = "2021-06-01 00:00:00";
        $endTime = date('Y-m-d H:i:s');
        if (!empty($data['time_enum']) && is_numeric($data['time_enum'])) {
            switch ($data['time_enum']) {
                //年
                case 1:
                    $startTime = date('Y-01-01 00:00:00');
                    $endTime = date('Y-m-d H:i:s');
                    break;
                //月
                case 2:
                    $startTime = date('Y-m-01 00:00:00');
                    $endTime = date('Y-m-d H:i:s');
                    break;
                //日
                case 3:
                    $startTime = date('Y-m-d 00:00:00');
                    $endTime = date('Y-m-d H:i:s');
                    break;
                //周
                case 4:
                    $week = date("w");
                    $week = $week ? $week - 1 : 6;
                    $startTime = date('Y-m-d 00:00:00', strtotime('-' . $week . ' day'));
                    $endTime = date('Y-m-d H:i:s');
                    break;
                default:
            }
        }
        $workEmployeeId = $user['workEmployeeId'];

        $salesClient = Db::table('sales_client_task_employee')
            ->when($workEmployeeId, function ($query) use ($workEmployeeId) {
                $query->where('work_employee_id', $workEmployeeId);
            })
            ->where('start_time', '>=', $startTime)
            ->where('end_time', '<', $endTime)
            ->sum('sales_task');

        $salesClientCompleted = Db::table('sales_task_employee')
            ->when($workEmployeeId, function ($query) use ($workEmployeeId) {
                $query->where('work_employee_id', $workEmployeeId);
            })
            ->where('start_time', '>=', $startTime)
            ->where('end_time', '<', $endTime)
            ->sum('completed');

        return [
            'totalTask' => $salesClient,
            'completed' => $salesClientCompleted,
        ];
    }

    /**
     * 验证规则.
     */
    protected function rules(): array
    {
        return [
            'time_enum' => 'in:1,2,3,4',
        ];
    }

    /**
     * 获取已定义验证规则的错误消息.
     */
    protected function messages(): array
    {
        return [
            'time_enum.in' => 'time_enum参数错误',
        ];
    }
}
